package ca.nfb.media.nfbPlayer.utils
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.nfb.media.nfbPlayer.views.NFBPlayerView;
	import ca.nfb.media.nfbPlayer.views.Sharing;
	import ca.turbulent.media.Pyro;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import ca.turbulent.media.events.PyroEvent;
	
	public class KeysHandler extends NFBPlayerView
	{
		
		private var currentPressedKeys:Object			= new Object();
		private var keysRegistered:Boolean				= false;
		
		public function KeysHandler()
		{
			super();		
		}
		
		override protected function addedToStage(evt:Event):void
		{
			registerStageEvents();
			registerKeys();
		}
		
		override protected function streamStarted(evt:PyroEvent):void
		{
			if (!keysRegistered)
				registerKeyEvents();
		}
		
		private function registerKeyEvents():void
		{
			if (stage.hasEventListener(KeyboardEvent.KEY_DOWN))
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, KD);
				
			if (stage.hasEventListener(KeyboardEvent.KEY_UP))
				stage.removeEventListener(KeyboardEvent.KEY_UP, KU);
				
			stage.addEventListener(KeyboardEvent.KEY_DOWN, KD, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_UP, KU, false, 0, true);
			
			keysRegistered = true;
		}
		
		private function registerKeys():void
		{
			currentPressedKeys["80"] = false; 	// -->> p
			currentPressedKeys["187"] = false;	// -->> +
			currentPressedKeys['189'] = false;	// -->> -
			currentPressedKeys['70'] = false;	// -->> f
			currentPressedKeys['16'] = false;	// -->> shift
			currentPressedKeys['84'] = false;	// -->> t
			currentPressedKeys['39'] = false;	// -->> right arrow
			currentPressedKeys['37'] = false;	// -->> left arrow
			currentPressedKeys['38'] = false;	// -->> up arrow
			currentPressedKeys['40'] = false;	// -->> down arrow
			currentPressedKeys['17'] = false;	// -->> ctrl / cmd
			currentPressedKeys['73'] = false;	// -->> i
			currentPressedKeys['69'] = false;	// -->> e
			currentPressedKeys['67'] = false;	// -->> c
			currentPressedKeys['68'] = false;	// -->> d
			currentPressedKeys['77'] = false;	// -->> m
			currentPressedKeys['66'] = false;	// -->> b
			currentPressedKeys['82'] = false;	// -->> r
			currentPressedKeys['83'] = false;	// -->> s
			currentPressedKeys['79'] = false;	// -->> o
			currentPressedKeys['74'] = false;	// -->> j
			currentPressedKeys['75'] = false;	// -->> k
		}
		
		private function KD(evt:KeyboardEvent):void
		{
			trace(evt.keyCode.toString());
			if (currentPressedKeys[evt.keyCode.toString()] != null)
				currentPressedKeys[evt.keyCode.toString()] = true;
			
			checkKeys();
		}
		
		private function KU(evt:KeyboardEvent):void
		{
			if (currentPressedKeys[evt.keyCode.toString()] != null)
				currentPressedKeys[evt.keyCode.toString()] = false;		
		}
		
		private function checkKeys():void
		{
			 if (currentPressedKeys["80"])
			 {
			 	if ((player.status == Pyro.STATUS_PLAYING || player.status == Pyro.STATUS_PAUSED) && 
			 												(core.currentState != NFBPlayer.MORE_INFOS_STATE && 
			 												core.currentState != NFBPlayer.OPTIONS_STATE && 
			 												core.currentState != NFBPlayer.SHARING_STATE))
			 	{
			 		player.togglePause();
			 	}
			 }
			 	
			 if (currentPressedKeys['187'])
			 {
			 	if (currentPressedKeys['189'])
			 		currentPressedKeys['189'] = false;
			 
			 	if (player.volume < 1)
			 		player.volume += .1;	
			 		
			 	core.controlBar.mapVolumeSlider();	
			 }
			 
			 if (currentPressedKeys['189'])
			 {
			 	if (player.volume > 0)
			 		player.volume -= .1;
			 		
			 	core.controlBar.mapVolumeSlider();		
			 }
			 
			 if (currentPressedKeys['77'])
			 {
			 	player.toggleMute();	
			 }
			 
			 if (currentPressedKeys['82'])
			 {	
			 	if ((player.status == Pyro.STATUS_PLAYING || player.status == Pyro.STATUS_PAUSED) && 
			 												(core.currentState != NFBPlayer.MORE_INFOS_STATE && 
			 												core.currentState != NFBPlayer.OPTIONS_STATE && 
			 												core.currentState != NFBPlayer.SHARING_STATE))
				{			 	
			 		player.seek(0.1);
			 	}	
			 }
			 
			 if (currentPressedKeys["70"])
			 {
			 	player.toggleFullScreen();
			 }
			 	
			 
			 if (currentPressedKeys["68"])
			  {
			  	if (player.status == Pyro.STATUS_PLAYING || player.status == Pyro.STATUS_PAUSED)
			  	{
			  		if (MovieClip(core.controlBar.descAudioButton_mc).mouseEnabled)
			  			core.toggleAudioDescription();
			  	}
			  }
			  	
			 	
			  if (currentPressedKeys["73"])
			  {
			 	if (core.currentState != NFBPlayer.MORE_INFOS_STATE && core.enableMoreInfosButton)
			 		core.setCurrentState(NFBPlayer.MORE_INFOS_STATE);
			  	else
			  		core.moreInfoView.close();
			  	
			  	return;
			  }
			  
			  if (currentPressedKeys["83"])
			  {
			  	if (core.currentState != NFBPlayer.SHARING_STATE && core.enableSharingButton)
			 		core.setCurrentState(NFBPlayer.SHARING_STATE);
			  	else
			  		core.sharingView.close();
			  		
			  	return;	
			  }
			  
			  if (currentPressedKeys["79"])
			  {
			  	if (core.currentState != NFBPlayer.OPTIONS_STATE && core.enableSpeedOptionsButton)
			 		core.setCurrentState(NFBPlayer.OPTIONS_STATE);
			  	else
			  		core.optionsView.close();
			  		
			  	return;	
			  }
			  
			  if (currentPressedKeys['66'])
			  {
			  	if (core.currentState == NFBPlayer.DEFAULT_STATE)
			  	{
			  		if (!core.controlBar.bookmarksView.bookmarkHolder_mc.visible)
			  			core.controlBar.bookmarksView.showAllBookmarks();		
			  		else
			  			core.controlBar.bookmarksView.hideAllBookmarks();
			  	}
			 }
			 
			 if (currentPressedKeys['75'])
			 {
			 	if (core.currentState == NFBPlayer.DEFAULT_STATE)
			 	{
			 		core.controlBar.tossForward();
			 	}
			 }
			 
			 if (currentPressedKeys['74'])
			 {
			 	if (core.currentState == NFBPlayer.DEFAULT_STATE)
			 	{
			 		core.controlBar.tossBackward();
			 	}
			 }
		}
		
	}
}